
          /*
           -------ProductDao By Raj Vallamsetla------
  		   
				

          */

package com.scalar.chiptrack.operations.process.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.operations.process.forms.FabProcessRecipeForm;
import com.scalar.chiptrack.utils.DBLoader;
import com.scalar.chiptrack.utils.SqlQueryMappings;

public class PCMDataDAO 
{

 private static DBLoader s_dbLoader = null;

 
     private PCMDataDAO()
	  {
	
	  }


	public static int savePCMData(DBTransaction dbTransaction,int processStepID,Iterator recipeList )throws ScalarException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		boolean status = false;
		int docID = 0;

		try{
			 if (dbTransaction == null) {
                            conn = DataSource.getConnection();
                        }
                        else {
                            conn = dbTransaction.getConnection();
                        }
            

		pstmt = conn.prepareStatement(SqlQueryMappings.get("operations.process.fab.insertRecipe"));

		FabProcessRecipeForm fabProcessRecipeForm = null;

			while(recipeList.hasNext()){
				fabProcessRecipeForm = (FabProcessRecipeForm)recipeList.next();
				pstmt.setInt(1, processStepID);

				//System.out.println("in Save PCMData "+fabProcessRecipeForm.getParameter());
				pstmt.setString(2, fabProcessRecipeForm.getParameter());
				pstmt.setString(3, fabProcessRecipeForm.getMinValue());
				pstmt.setString(4, fabProcessRecipeForm.getMaxValue());
							
				pstmt.addBatch();
				
			}
			pstmt.executeBatch();

			status = true;

			pstmt.close();
			pstmt=null;
			pstmt = conn.prepareStatement(SqlQueryMappings.get("operations.process.upload.readIdentity"));
			rs = pstmt.executeQuery();

			if(rs.next() != false){
				docID = rs.getInt("docID");
			}

	
	}catch(SQLException sql){
			sql.printStackTrace();
			throw new ScalarException(" upload failed", sql);

		}finally{

			// Commit/Rollback here it selef -- if its not in a manual transaction
                        if (dbTransaction == null) {
                            try {
                                if (conn != null) {
                                    if (status == true) {
                                        conn.commit();
                                    }
                                    else {
                                        conn.rollback();
                                    }
                                }
                            }
                            catch (SQLException ignore) {}
                        }
                        
			if(pstmt != null){
				try{ pstmt.close(); }catch(Exception e){}
			}
			if(conn != null){
				try{ conn.close(); }catch(Exception e){}
			}

		}
		return docID;

	}


	// Delete PCM Data for existing process step id	

	public static void deletePCMData (DBTransaction dbTransaction, int processStepID) throws ScalarException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		boolean status = false;

		try{
                    // Get the connection from regular datasource if its not in a manual transaction
                    if (dbTransaction == null) {
                        conn = DataSource.getConnection();
                    }
                    else {
                        conn = dbTransaction.getConnection();
                    }

                    pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.fab.deleteRecipe"));
					pstmt.setInt(1,processStepID);
                    pstmt.executeUpdate();
                    status = true;

		}

                catch(Exception sql) {
                    sql.printStackTrace();
                    throw new ScalarException("Could not access VendorLocationList", sql);
		}

                finally{
                    // Commit/Rollback here it selef -- if its not in a manual transaction
                    if (dbTransaction == null) {
                        try {
                            if (conn != null) {
                                if (status == true) {
                                    conn.commit();
                                }
                                else {
                                    conn.rollback();
                                }
                            }
                        }
                        catch (SQLException ignore) {}
                    }
                        
                    if(pstmt != null){
                        try{pstmt.close();}catch(SQLException se){}
                    }
                    if(conn != null){
                        try{ conn.close(); }catch(SQLException se){}
                    }			
		}
	}	

	
		

}	